/*
 * Copyright (c) 2010-2011 NOO. All Rights Reserved.
 * [Id:LoginBean.java  11-10-5 下午10:42 poplar.yfyang ]
 */
package org.mumu.pawn.beans.auth;

import org.jboss.logging.Logger;
import org.mumu.pawn.beans.common.AccountSessionBean;
import org.mumu.pawn.domain.Module;
import org.mumu.pawn.domain.Operate;
import org.mumu.pawn.domain.Role;
import org.mumu.pawn.domain.User;
import org.mumu.pawn.model.Account;
import org.mumu.pawn.model.PanelMenu;
import org.mumu.pawn.cmps.UserCmp;
import org.mumu.pawn.util.FacesUtil;
import org.mumu.pawn.util.MD5Util;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 登录请求Bean.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 11-10-5 下午10:42
 * @since JDK 1.5
 */
@Named("authentication")
@RequestScoped
public class AuthenticationBean implements Serializable {
    private static final long serialVersionUID = 200207542536343278L;


    /**
     * 登录请求,成功后进行重定向到首页
     *
     * @return 返回重定向页面名称, faces-redirect=true表示重定向.
     */
    public String userLogin() {
        if (user.getUserName() == null || user.getPassword() == null) {
            log.error("用户名活着密码不能为空");
            FacesUtil.sendMessage("用户名活着密码不能为空");
            return null;
        }
        String password = user.getPassword();
        user = userCmp.findUser(user.getUserName());
        if (user == null) {
            log.error("用户不存在");
            FacesUtil.sendMessage("用户不存在");
            return null;
        }
        String mdPassword = MD5Util.getInstance().getMD5ofStr(password);
        if (!mdPassword.equals(user.getPassword())) {
            log.error("密码错误!");
            FacesUtil.sendMessage("密码错误!");
            return null;
        }
        setAccountSession();
        setAccountModuleMenu();
        return "main?faces-redirect=true";
    }

    /**
     * 退出系统。
     *
     * @return 退出系统登录
     */
    public String logout() {
        HttpSession session = FacesUtil.faceSession();
        session.invalidate();
        return "login?faces-redirect=true";
    }

    /**
     * 设置Session中的用户操作对象
     */
    private void setAccountSession() {
        Account account = new Account();
        account.setId(user.getId());
        account.setGender(user.getGender() == 0 ? "男" : "女");
        account.setNickName(user.getNickName());
        account.setPassword(user.getPassword());
        account.setUserName(user.getUserName());
//        account.setDepartment(user.getUsrPowerInfo().getDepartment());
//        account.setRule(user.getUsrPowerInfo().getRule());
        accountSessionBean.setAccount(account);
    }

    /**
     * 构建一个菜单项
     */
    private void setAccountModuleMenu() {
        List<PanelMenu> menus = new ArrayList<PanelMenu>();
        PanelMenu menu, parentMenu;
        List<PanelMenu> childMenu;
        for (Role role : user.getRoles()) {
            for (Module module : role.getModules()) {
                childMenu = new ArrayList<PanelMenu>();
                parentMenu = new PanelMenu();
                parentMenu.setId(module.getModuleName());
                parentMenu.setLabel(module.getModuleName());
//                parentMenu.setLink(module.getModuleUrl());
                for (Operate operate : module.getOperates()) {
                    menu = new PanelMenu();
                    menu.setId(operate.getOperateSign());
                    menu.setLabel(operate.getOperateName());
                    menu.setLink(operate.getOperateAction());
                    childMenu.add(menu);
                }
                parentMenu.setChildMenu(childMenu);
                menus.add(parentMenu);
            }
        }
        accountSessionBean.setMenus(menus);
    }


    @Inject
    private UserCmp userCmp;

    @Inject
    private Logger log;


    @Inject
    private AccountSessionBean accountSessionBean;


    private User user = new User();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
